#include <stdio.h>
#pragma warning(disable:4996)
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PASSWORD 6666
int main(void)

{
	int i, k, num, x, count, coin;

	int b[100], a[100], full[100], temp[100] = { 0 };

	int password, flag = 0;

	srand((int)time(0));

	printf("寄存柜数量:");

	scanf("%d", &num);

	printf("\n");
	while (num <= 0) {
		printf("寄存柜数量错误，请重新输入\n\n");
		printf("寄存柜数量：");
		scanf("%d", &num);
		printf("\n");
	}
	for (i = 0; i < num; i++)
		full[i] = 0;

	/*---------*/

	count = 0;

	while (1) {
		printf("1.投硬币   2.输密码   0.退出请选择:\n");
		printf("请输入>>");
		scanf("%d", &x);

		printf("\n");

		/*-------*/

		if (x == 1) {
			printf("投币值:");

			scanf("%d", &coin);

			printf("\n");

			if (coin == 1) {
				if (count < num)
				{
					b[count] = rand() % num + 1;

					while (full[b[count] - 1] == 1) {
						b[count] = rand() % num + 1;

						for (i = 0; i < count; i++)
						{
							if (b[count] == b[i]) {
								b[count] = rand() % num + 1;

								i = 0;

							}

						}

					}

					full[b[count] - 1] = 1;

					a[count] = rand() % 9000 + 1000;

					for (i = 0; i < count; i++)
					{
						if (a[count] == a[i]) {
							a[count] = rand() % 9000 + 1000;

							i = 0;

						}

					}
					printf("寄存箱编号:%d 密码:%d\n\n", b[count], a[count]);
					count++;
				}
				else
					printf("本柜已满\n\n");
			}
			else {
				printf("投错硬币, 请重新选择\n\n");
				continue;
			}
		}
		else if (x == 2) {
			printf("输入密码:");
			scanf("%d", &password);
			printf("\n");
			if (count == 0) {
				printf("密码错误\n\n");
				continue;
			}
			for (i = 0; i < count; i++)
			{
				if (full[b[i] - 1] == 1 && password == a[i]) {
					printf("%d号寄存箱已打开\n\n", b[i]);

					full[b[i] - 1] = 0;

					flag = 1;

					for (k = i; k < count - 1; k++)
					{
						b[k] = b[k + 1];

						a[k] = a[k + 1];

						full[b[k] - 1] = full[b[k + 1] - 1];

					}
					break;

				}

			}

			if (flag != 1)

				printf("密码错误\n\n");

			else {
				count--;

				flag = 0;

			}

		}
		else if (x == 5) {
			int flag = 0;
			printf("请输入万能密码: ");
			scanf("%d", &password);
			if (password == PASSWORD) {
				for (i = 0; i < count; i++) {
					if (full[b[i] - 1] == 1) {
						temp[flag++] = b[i];
					}

				}
			}
			printf("箱子已经全部打开");
			if (flag)
				printf("请重点检查");
			for (i = 0; i < flag; i++)
				printf("编号%d ", temp[i]);
			if (flag)
				printf("是否有物存留\n");
			else
				printf("\n");
		}

		else if (x == 0)

			break;

		else

			printf("输入错误, 请重新输入\n\n");

	}

	return 0;

}